package com.linewow.xhyy.nihaonew.view.input;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.inter.menu.PlusInter;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

/**
 * Created by LXR on 2016/9/2.
 */
public class ImgAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder>{
    private List<String> lists;
    private Context context;
    private String TAG="ImgAdapter";
    private PlusInter inter;
    private LayoutInflater layoutInflater;
    public enum ITEM_TYPE {
        ITEM1,
        ITEM2
    }

    public ImgAdapter(List<String> lists, Context context,PlusInter inter) {
        this.lists = lists;
        this.context = context;
        this.inter= inter;
        layoutInflater= LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType== ITEM_TYPE.ITEM2.ordinal()){
            View view=layoutInflater.inflate(R.layout.item_recycler_view,null);
            return new FootViewHolder(view);
        }else{
            View view=layoutInflater.inflate(R.layout.item_recycler_view,null);
            return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof FootViewHolder){
            Picasso.with(context).load(R.mipmap.bg_plus).fit().into(((FootViewHolder) holder).imageView);
        }else if(holder instanceof ViewHolder){
            String path=lists.get(position);
            Picasso.with(context).load(new File(path)).fit().centerCrop().into(((ViewHolder)holder).imageView);
        }

    }

    @Override
    public int getItemViewType(int position) {
        return  (position==lists.size())? ITEM_TYPE.ITEM2.ordinal(): ITEM_TYPE.ITEM1.ordinal();
    }
    public void remove(int position){
        lists.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public int getItemCount() {
        return lists.size()+1;
    }

    public class ViewHolder extends  RecyclerView.ViewHolder{
        public ImageView imageView;
        public ImageView delImg;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.item_src);
            delImg= (ImageView) itemView.findViewById(R.id.item_del);
            delImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(getAdapterPosition()!=lists.size()){
                        remove(getAdapterPosition());
                    }
                }
            });
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder{

        public ImageView imageView;
        public ImageView delImg;
        public FootViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.item_src);
            delImg= (ImageView) itemView.findViewById(R.id.item_del);
            delImg.setVisibility(View.GONE);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    inter.plus();
                }
            });
        }
    }
}
